package edu.ccut.titan.service.impl;

import java.util.List;

import org.osgi.framework.Bundle;

import edu.ccut.titan.component.ComponentFactory;
import edu.ccut.titan.component.ComponentManager;
import edu.ccut.titan.component.IBundleInfo;
import edu.ccut.titan.component.IDirectoryInfo;
import edu.ccut.titan.component.TitanComponentException;
import edu.ccut.titan.service.ITitanConstants;
import edu.ccut.titan.service.ITitanManagerService;

/**
 * 
 * @author LeslieGu
 */
public class TitanManagerService implements ITitanManagerService {

	private ComponentManager componentManager;

	public TitanManagerService() {
		componentManager = ComponentManager.getInstance();
	}

	public void addBundle(Bundle bundle) {
		IBundleInfo bundleInfo = ComponentFactory
				.createBundleComponentInfo(bundle);
		
		bundleInfo.setWebRootPath(TitanPropertiesService.getInstance().getProperty(ITitanConstants.TITAN_SERVER_LOCATION));

		List<IDirectoryInfo> directoryInfos = ComponentFactory
				.getDirectoryInfos(bundle);

		for (IDirectoryInfo directoryInfo : directoryInfos) {
			try {
				directoryInfo.load(bundle, bundleInfo);
			} catch (TitanComponentException e) {
				e.printStackTrace();
			}
		}

		this.componentManager.addBundleComponentInfo(bundleInfo);
	}

	public void removeBundle(Bundle bundle) {
		IBundleInfo bundleInfo = ComponentFactory
				.createBundleComponentInfo(bundle);

		List<IDirectoryInfo> directoryInfos = ComponentFactory
				.getDirectoryInfos(bundle);

		for (IDirectoryInfo directoryInfo : directoryInfos) {
			try {
				directoryInfo.unload(bundle, bundleInfo);
			} catch (TitanComponentException e) {
				e.printStackTrace();
			}
		}

		this.componentManager.removeBundleComponentInfo(bundle
				.getSymbolicName());
	}

//	private IBundleInfo loadBundleComponentInfo(Bundle bundle) {
//		IBundleInfo bundleComponentInfo = ComponentFactory
//				.createBundleComponentInfo(bundle);
//
//		try {
//			bundleComponentInfo.addComponentInfos(this
//					.loadBaseComponentInfo(bundle));
//
//			bundleComponentInfo.addComponentInfos(this
//					.loadLogicComponentInfo(bundle));
//			bundleComponentInfo.addComponentInfos(this
//					.loadActionComponentInfo(bundle));
//
//		} catch (TitanComponentException e) {
//			e.printStackTrace();
//		}
//
//		return bundleComponentInfo;
//	}
//
//	private List<IComponentInfo> loadBaseComponentInfo(Bundle bundle)
//			throws TitanComponentException {
//
//		return ComponentFactory.getComponentInfos(bundle,
//				TITAN_COMPONENT_FILE_NAME);
//	}

//	@SuppressWarnings("unchecked")
//	private List<IComponentInfo> loadLogicComponentInfo(Bundle bundle)
//			throws TitanComponentException {
//
//		List<IComponentInfo> logicComponentInfos = new ArrayList<IComponentInfo>();
//
//		List<IDirectoryInfo> directoryInfos = ComponentFactory
//				.getDirectoryInfos(bundle);
//
//		for (IDirectoryInfo directoryInfo : directoryInfos) {
//			if (IDirectoryInfo.TITAN_DIRECTORY_TYPE_LOGIC.equals(directoryInfo
//					.getType())) {
//				Enumeration<String> logicFiles = bundle
//						.getEntryPaths(directoryInfo.getDirectoryPath());
//
//				while (logicFiles.hasMoreElements()) {
//					String logicFile = logicFiles.nextElement();
//
//					if (logicFile != null
//							&& logicFile
//									.endsWith(TITAN_LOGIC_COMPONENT_FILE_POSTFIX)) {
//						logicComponentInfos.add(ComponentFactory
//								.getLogicComponentInfo(bundle, logicFile));
//					}
//				}
//			}
//		}
//
//		return logicComponentInfos;
//	}
//
//	@SuppressWarnings("unchecked")
//	private List<IComponentInfo> loadActionComponentInfo(Bundle bundle)
//			throws TitanComponentException {
//
//		List<IComponentInfo> actionComponentInfos = new ArrayList<IComponentInfo>();
//
//		List<IDirectoryInfo> directoryInfos = ComponentFactory
//				.getDirectoryInfos(bundle);
//
//		for (IDirectoryInfo directoryInfo : directoryInfos) {
//			if (IDirectoryInfo.TITAN_DIRECTORY_TYPE_ACTION.equals(directoryInfo
//					.getType())) {
//				Enumeration<String> actionFiles = bundle
//						.getEntryPaths(directoryInfo.getDirectoryPath());
//
//				while (actionFiles.hasMoreElements()) {
//					String actionFile = actionFiles.nextElement();
//
//					if (actionFile != null
//							&& actionFile
//									.endsWith(TITAN_ACTION_COMPONENT_FILE_POSTFIX)) {
//						actionComponentInfos.add(ComponentFactory
//								.getActionComponentInfo(bundle, actionFile));
//
//					}
//				}
//			}
//		}
//
//		return actionComponentInfos;
//	}
}
